package org.springcloud.wisdom.spring_02_classroom.student.attendence.service;

import org.springcloud.wisdom.spring_02_classroom.bean.Page;
import org.springcloud.wisdom.spring_02_classroom.student.attendence.dao.AttendenceDao;
import org.springcloud.wisdom.spring_02_classroom.student.attendence.entity.AttendenceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendenceService {
    @Autowired
    private AttendenceDao attendenceDao;

    //学生查看考勤表
    public List<AttendenceEntity> attendenceBySidAndCid(String sid) {
        if (sid == null || sid.trim().length() == 0){
            return null;
        }
        return  attendenceDao.attendenceBySidAndCid(sid);
    }

    //教师查看考勤表
    public Page<AttendenceEntity> AllAttendence(int pageNum, int pageSize, String cid,String sname) {
        // 计算起始位置
        int start = (pageNum - 1) * pageSize;
        // 获取分页的数据
        List<AttendenceEntity> list = attendenceDao.get(cid,sname ,start, pageSize);
        // 获取总条目数
        int total = attendenceDao.Count(cid,sname);
        // 计算总页数
        int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        // 创建分页对象
        Page<AttendenceEntity> page = new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}
